package com.dhcc.cdc.base.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.base.entity.CdcResulttypeEntity;
import com.dhcc.cdc.base.service.ICdcResulttypeService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 结果类型
 * 
 * @ClassName: CdcResulttypeController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:19:38
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcresulttype")
public class CdcResulttypeController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcresulttype/";

	@Autowired
	private ICdcResulttypeService cdcResulttypeService;
	

    /**
     * 跳转到结果类型列表首页
     */
    @RequestMapping("")
    @Permission("cdcresulttype:list")
    public String index() {
        return PREFIX + "cdcresulttype.html";
    }
	
    /**
     * 跳转到新增结果类型
     */
    @RequestMapping("/to_add")
    @Permission("cdcresulttype:add")
    public String toAdd() {
        return PREFIX + "cdcresulttype_add.html";
    }
	
	/**
     * 跳转到修改结果类型
     */
    @RequestMapping("/to_update/{cdcResulttypeId}")
    @Permission("cdcresulttype:update")
    public String toUpdate(@PathVariable Long cdcResulttypeId, Model model) {
        CdcResulttypeEntity cdcResulttype = this.cdcResulttypeService.selectById(cdcResulttypeId);
        model.addAttribute("cdcResulttype",cdcResulttype);
        return PREFIX + "cdcresulttype_edit.html";
    }

    /**
     * 获取结果类型列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcresulttype:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcResulttypeEntity> page = new PageFactory<CdcResulttypeEntity>().defaultPage();
        List<CdcResulttypeEntity> result = this.cdcResulttypeService.page(page,params);
        page.setRecords((List<CdcResulttypeEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增结果类型
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcresulttype:add")
    @BussinessLog(value = "新增结果类型")
    public Object add(CdcResulttypeEntity cdcResulttype) {
        if (CommonUtil.isOneEmpty(cdcResulttype)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcResulttype.insert();
        return R.ok();
    }

    /**
     * 删除结果类型
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcresulttype:delete")
    @BussinessLog(value = "删除结果类型")
    public Object delete(@RequestParam Long cdcResulttypeId) {
	
        this.cdcResulttypeService.deleteById(cdcResulttypeId);

        return R.ok();
    }

    /**
     * 修改结果类型
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcresulttype:update")
    @BussinessLog(value = "修改结果类型")
    public Object update(CdcResulttypeEntity cdcResulttype) {
        if (CommonUtil.isOneEmpty(cdcResulttype)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcResulttypeEntity old = this.cdcResulttypeService.selectById(cdcResulttype.getId());

        old.setCode(cdcResulttype.getCode());
        old.setDescription(cdcResulttype.getDescription());
        old.setNote(cdcResulttype.getNote());
        old.setIsActive(cdcResulttype.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
